
from app.core.exception import ApiException
from app.entity.projectDetails import ProjectDetails
from app.service.project import ProjectService
from app.service.projectUser import ProjectUserService
from app.service.user import UserService
from app.utils.RequestUtils import RequestUtils


class ProjectDetailsService:
    
    def getGroupDetailCount(groupId):
        return ProjectDetails().query().where(ProjectDetails.payGroupId == groupId).count()
    
    def createProjectDetails(projectDetailData, projectId):
        from app.service.project import ProjectService
        if projectId==None or projectId<=0:
            raise ApiException("项目不存在")
        project=ProjectService.existsProject(projectId)
        pd=ProjectDetails()
        pd.projectId=projectId
        pd.title=projectDetailData['title']
        pd.remask=projectDetailData['remask'] if 'remask' in projectDetailData else ''
        pd.price=projectDetailData['price']
        pd.payDate=projectDetailData['payDate']
        pd.payer=projectDetailData['payer']
        if pd.payer<=0:
            pd.customPayer=projectDetailData['payer_desc']
        pd.payGroupId=projectDetailData['payGroupId']
        pd.createdBy=projectDetailData['createdBy']
        ProjectDetailsService.checkAuth(pd,RequestUtils().getCurrentUserId())
        pd.save()
        return pd
    
    def updateProjectDetails(projectDetailData):
        pd=ProjectDetailsService.existsProjectDetail(projectDetailData['id'])
        pd.title=projectDetailData['title']
        pd.remask=projectDetailData['remask'] if 'remask' in projectDetailData else ''
        pd.price=projectDetailData['price']
        pd.payDate=projectDetailData['payDate']
        pd.payer=projectDetailData['payer']
        if pd.payer<=0:
            pd.customPayer=projectDetailData['payer_desc']
        pd.payGroupId=projectDetailData['payGroupId']
        pd.save()
        return pd

    def deleteProjectDetails(projectDetailId):
        pd=ProjectDetailsService.existsProjectDetail(projectDetailId)
        currentUserId=RequestUtils().getCurrentUserId()
        ProjectDetailsService.checkAuth(pd,currentUserId)
        pd.delete()
        pd.commit()

    def listProjectDetails(projectId,query=None)->list[ProjectDetails]:
        pageData=RequestUtils().getPageData()
        return ProjectDetails().query().where(ProjectDetails.projectId == projectId).order_by(ProjectDetails.payDate.desc(),ProjectDetails.createdAt.desc())\
            .limit(pageData['page_count']).offset(pageData['offset']).all()

    def existsProjectDetail(projectDetailId)->ProjectDetails:
        pd=ProjectDetails().query().where(ProjectDetails.id == projectDetailId).first()
        if pd==None:
            raise ApiException("账目不存在")
        return pd
    
    def checkAuth(pd:ProjectDetails,currentUserId):
        user=UserService.isValidUser(currentUserId)
        if user.isSuperAdmin=="Y":
            return True
        projectUser=ProjectUserService.getProjectUser(pd.projectId,user.id)
        if projectUser==None:
            raise ApiException("您没有权限修改该账目")
        if projectUser.isAdmin=="Y":
            return True
        if pd.payGroupId==projectUser.defalutGroupId:
            return True
        if pd.createdBy==currentUserId:
            return True
        raise ApiException("您没有权限修改该账目")
        
        